package io.renren.modules.app.api;

import io.renren.common.exception.RRException;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.UserLevelVo;
import io.renren.modules.app.entity.UserVo;
import io.renren.modules.app.service.ApiUserLevelService;
import io.renren.modules.app.service.ApiUserService;
import io.renren.modules.oss.service.SysOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 会员api
 * @author zzh
 * @email 862583291@qq.com
 * @date 2018-12-31 12:27
 */
@Api("会员api")
@RestController
@RequestMapping("/app/v1/user")
public class ApiUserController {

    @Autowired
    private ApiUserService userService;
    @Autowired
    private ApiUserLevelService apiUserLevelService;

    @Autowired
    private SysOssService sysOssService;
    /**
     * 获取用户信息
     * @param user
     * @return
     * @CrossOrigin 解决跨域
     */
    @Login
    @GetMapping("userInfo")
    @ApiOperation("获取用户信息")
    public R userInfo(@LoginUser UserVo user){
        if (!StringUtil.isNullOrEmpty(user.getUserLevelId())) {
            UserLevelVo userLevel = apiUserLevelService.getById(user.getUserLevelId());
            if (!StringUtil.isNullOrEmpty(userLevel)) {
                user.setUserLevel(userLevel.getName());
            }
        }
        return R.ok("成功获取用户信息").put("data", user);
    }

    /**
     * 修改用户
     */
    @Login
    @PostMapping("/update")
    @ApiOperation("修改用户")
    public R update(@RequestBody UserVo user){
        ValidatorUtils.validateEntity(user, UpdateGroup.class);
        userService.updateById(user);
        return R.ok("操作成功");
    }

    /**
     * 获取融云token
     */
    @PostMapping("/getToken")
    public R getToken(@RequestBody UserVo user){
        ValidatorUtils.validateEntity(user, UpdateGroup.class);
        userService.updateById(user);
        return R.ok("操作成功");
    }

    /**
     * 编辑头像
     */
    @Login
    @PostMapping("/avatarEdit")
    @ApiOperation("编辑头像")
    public R avatarEdit(@RequestParam("file") MultipartFile file,@RequestParam Long userId){
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        if (StringUtil.isNullOrEmpty(userId)) {
            throw new RRException("上传文件不能为空");
        }
        UserVo user = userService.getById(userId);
        String oldAvatar = user.getAvatar();
        String url = null;
        try {
            url = sysOssService.upload(file);
            user.setAvatar(url);
            userService.updateById(user);
            if(!StringUtil.isNullOrEmpty(oldAvatar)){
                sysOssService.deleteByUrl(oldAvatar);
            }
        } catch (Exception e) {
            new RRException(e.getMessage());
        }
        return R.ok().put("data", user);
    }

}
